package com.mc.moring.util.ext

import android.content.Context
import java.text.NumberFormat
import java.util.Locale

object StringExt {

    fun Context?.extString(resId: Int): String? {
        this ?: return null
        return this.getString(resId)
    }

    fun Context?.extString(resId: Int, vararg formatArgs: Any): String? {
        this ?: return null
        return java.lang.String.format(
            this.getString(resId),
            *formatArgs
        )
    }

    fun String?.extEquals(target: String?): Boolean {
        if (this.isNullOrEmpty() || target.isNullOrEmpty()) return false
        return this == target
    }

    fun Int?.extFormatMoney(): String {
        if (this == null) {
            return "0"
        }
        return this.toString().extFormatMoney()
    }

    fun Float?.extFormatMoney(): String {
        if (this == null) {
            return "0"
        }
        return this.toString().extFormatMoney()
    }

    fun Double?.extFormatMoney(): String {
        if (this == null) {
            return "0"
        }
        return this.toString().extFormatMoney()
    }

    fun String?.extFormatMoney(): String {
        if (this.isNullOrEmpty()) {
            return "0"
        }
        val value = try {
            this.toDouble()
        } catch (e: Exception) {
            return "0"
        }
//        return StringUtils.formatNumberWithCommas(value.toString())
        return NumberFormat.getNumberInstance(Locale("es","CL")).apply {
            isGroupingUsed = true
        }.let {
            it.format(value)
        }
    }

    fun String?.extHidePhone(): String {
        if (this.isNullOrEmpty()) return ""
        if (this.length <= 4) {
            return this
        }
        return "*****${this.substring(length - 4, length)}"
    }

    fun String?.extAlso(call: String.() -> Unit) {
        if (isNullOrEmpty()) return
        call.invoke(this)
    }

    fun String.isAllWhitespace(): Boolean {
        return this.all { it.isWhitespace() }
    }


}